import java.util.Random;

// Tabs = 4
//************************************************************
//	COPYRIGHT 2011 Bread Zeppelin - ALL RIGHTS RESERVED
//
// This file is the product of Bread Zeppelin and cannot be 
// reproduced, copied, or used in any shape or form without 
// the express written consent of Bread Zeppelin.
//************************************************************
//
//	$ID: Concurrent_Assg6
//
//	$Date: Feb 13, 2012
//
//	Reason:
//
//*************************************************************

/** $Author: Ben Hare, Zachary Huenink, Philipp Wittmaack, Peter Maresca */
/** $Revision: 1 */


/**
 * 
 *
 */
public class AcceptCustomer_Task implements Runnable {

	//Runnable
	final CustomerParty customerParty;
	Restaurant restaurant = Restaurant.getInstance();
	WaitingList waitList = restaurant.getWaitingList();
	TablePool tablePool = restaurant.getTablePool();

	/**
	 * @param customerParty
	 */
	AcceptCustomer_Task(CustomerParty customerParty){
		this.customerParty = customerParty; 

	}

	public void run() {
		
		this.customerParty.walkIn();
		
		// Initiate WaitTime Metric
		RestaurantMetrics.getInstance().beginWaitTime(customerParty, System.currentTimeMillis());

		int time = new Random(System.currentTimeMillis()).nextInt(8000);
		try { 
			Thread.sleep(time/Simulator.simulationSpeed);
			if(time > 7500){
				Thread.currentThread().interrupt();
			}

			waitList.addCustomer(customerParty);
			System.out.println("Customer: " + customerParty.getName() + ". Party of: " +
					customerParty.getPartySize() +". \n	Added To waiting list");

			// Should be MOVED to executor HOOKS. =============================================================<		
			SeatCustomer_Task seatTask = new SeatCustomer_Task(customerParty);
			restaurant.getHostessExecutor().execute(seatTask);

		} 
		catch (InterruptedException e) {
			customerParty.rageQuit();
			System.out.println(customerParty.getName() + " has left after" +
			" waiting too long to see the hostess");
		}

	}
}
